﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="GetDataSetFromCSV.cs" company="Collaboris Ltd.">
//   Copyright (c) Collaboris Ltd. All rights Reserved.
// </copyright>
// <summary>
//   Reads the csv sheet and returns a DataSet object.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Collaboris.Saf.Actions.DataSet
{
    using System.Collections.Generic;

    
    using Engine;

    using SharePoint.Wss.Attributes;

    using Utils;
    using System.Data;

    /// <summary>
    /// The get files.
    /// </summary>
    [CollaborisClass(
          Title = "Gets a DataSet from a CSV File",
          ReleaseStatus = ReleaseStatus.Development,
          DisableDocumentation = false,
          ContactAuthorEmail = "Support@Collaboris.co.uk",
          Summary = "This action is responsible for getting a DataSet from a Comma Seperated File.",
          Remarks = "The DataSet is stored in a property within the entity called 'CsvDataSet'. " + 
                    "This can then be passed into subsequent actions using output properties.",
          ClassGuid = "{156E17A5-A7BA-4e1f-8A48-1BC341A7C612}",
          SupportFiles = new string[] { @"Actions\DataSet\TestTasks.csv"}
      )
    ]
    public class GetDataSetFromCSV : ActionBase<Entities.CSVFileInfo>
    {
        /// <summary>
        /// Executes the Action using the values passed in by the Data Entity
        /// and the <see cref="SafContext"/>.
        /// </summary>
        /// <param name="context">
        /// A valid <see cref="SafContext"/> containing properties to run the Action with.
        /// </param>
        /// <param name="entity">
        /// A Valid <see cref="T"/> containing the parameters with which to run the Action with.
        /// </param>
        /// <returns>
        /// A <see cref="ProcessStatus"/> signifying the outcome of processing the Action.
        /// </returns>
        protected override ProcessStatus DoAction(SafContext context, Entities.CSVFileInfo entity)
        {
            this.Trace.TraceMethodStart("GetDataSetFromCSV.DoAction");

            this.Trace.TraceInfo("Getting the DataSet object for the csvSheet:{0} - TableName: {1}", 
                                    entity.FilePath,entity.TableName);

            DataSet dataSet = CsvHelper.GetDataSetFromCSV(entity.FilePath, entity.TableName);
            if (dataSet == null)
            {
                this.Trace.TraceError("Failed to get the dataset object from the CSV File:{0}", entity.FilePath);
                return ProcessStatus.Error;
            }

            // Assign the output to the entity...
            entity.CsvDataSet = dataSet;

            this.Trace.TraceMethodEnd("GetDataSetFromCSV.DoAction");
            return ProcessStatus.Success;
        }


        /// <summary>
        /// Checks the Data entity used by the Action for Validity
        /// </summary>
        /// <param name="context">
        /// The <see cref="SafContext"/> passed in by the SAF Engine.
        /// </param>
        /// <param name="entity">
        /// The data entity to Validate.
        /// </param>
        /// <param name="errors">
        /// List of <see cref="ValidationError"/>s
        /// </param>
        /// <param name="mode">
        /// <see cref="ProcessMode"/>signalling wheter we are Doing, or Undoing the action.
        /// </param>
        /// <returns>
        /// false if invalid; otherwise true.
        /// </returns>
        protected override bool IsValid(
                SafContext context, Entities.CSVFileInfo entity, ref List<ValidationError> errors, ProcessMode mode)
        {
            this.Trace.TraceMethodStart("IsValid");

            int currentCount = errors.Count;

            // Validate each object now.
            ValidateStringForNull(entity.FilePath, "CSVFileInfo.FilePath", ref errors);
            ValidateStringForNull(entity.TableName, "CSVFileInfo.TableName", ref errors);
            this.Trace.TraceMethodEnd("IsValid");

            // Have we added errors ?
            return errors.Count == currentCount;
        }
    }
}